//
//  SendNoticeViewController.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-8-1.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "SendNoticeViewController.h"
#import "NetworkAccessManager.h"
#import "AppDelegate.h"
#import "UIAlertView+Kirinrin.h"
#import <UIActivityIndicatorView+AFNetworking.h>

@interface SendNoticeViewController ()<UITextFieldDelegate,UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *contentTextView;
@property (weak, nonatomic) IBOutlet UITextField *titleTextView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *waiter;

@end

@implementation SendNoticeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewDidAppear:(BOOL)animated{
    [self.titleTextView becomeFirstResponder];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)sendNoticeAction:(id)sender {
    
    NSString *textcontent = [self.contentTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *title = [self.titleTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (textcontent.length == 0) {
        [UIAlertView alertWithFormCheckError:FCErrorInvaildLength];
        return;
    }
    if (self.titleTextView.text.length == 0) {
        [UIAlertView alertWithFormCheckError:FCErrorInvaildLength];
        return;
    }
    
    [self.view endEditing:YES];
    self.view.userInteractionEnabled = NO;
    
    AppDelegate *delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    NSString *uriString = [NSString stringWithFormat:@"Notice.json"];
    NSDictionary *parameters = @{@"key": delegate.accessKey, @"content":textcontent, @"kClassId":delegate.user[@"kClassId"],@"title":self.titleTextView.text};
    
    AFHTTPRequestOperation *operation = [[NetworkAccessManager shareManager]POST:uriString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"opernation: %@\nresponse: %@", operation, responseObject);
        
        int resultCode = [responseObject[@"result"]intValue];
        if (resultCode == 0) {
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            [UIAlertView alertWithRemoteError:resultCode];
        }
        self.view.userInteractionEnabled = YES;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"opernation: %@\nerror: %@", operation, error);
        [UIAlertView alertWithNetworkError];
        self.view.userInteractionEnabled = YES;
    }];
    
    [_waiter setAnimatingWithStateOfOperation:operation];
}


#pragma mark - UITextViewDelegate
-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    
    if (textView.text.length == 0) {
        textView.text = @"请输入通知内容";
    }
    
    return YES;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@"请输入通知内容"]) {
        textView.text = @"";
    }
    
    return YES;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    
    if (textField.text.length == 0 ) {
        textField.text = @"请输入标题";
    }
    return YES;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if ([textField.text isEqualToString:@"请输入标题"]) {
        textField.text = @"";
    }
    return YES;
}
@end
